//
//  ZHAnchorMoreViewController.m
//  XMLYFM
//
//  Created by dllo on 16/1/4.
//  Copyright © 2016年 zzl. All rights reserved.
//

#import "ZHAnchorMoreViewController.h"
#import "ZHPlayViewController.h"
#import "ZHPlayButtonView.h"
#import "ZHAnchorDetailViewController.h"
#import "AppTools.h"
#import "ZHListModel.h"
#import "ZTListTableViewCell.h"

#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

@interface ZHAnchorMoreViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) NSMutableArray *titleArray;

@property (nonatomic, strong) NSMutableArray *topListArray;

@property (nonatomic, strong) NSMutableArray *imageArray;

@property (nonatomic, strong) UITableView *listTableView;


@end

@implementation ZHAnchorMoreViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[ZHPlayButtonView shareButton].musicBtn addTarget:self action:@selector(buttonActionz:) forControlEvents:UIControlEventTouchUpInside];
    
    if ([ZHPlayViewController sharaMusicPlayer].player.state == STKAudioPlayerStatePlaying) {
        
        [[ZHPlayButtonView shareButton] startCircle];
    }
    if ([ZHPlayViewController sharaMusicPlayer].player.state == STKAudioPlayerStatePaused) {
        
        [[ZHPlayButtonView shareButton] stopCircle];
        
    }
    
    [self.view bringSubviewToFront:self.messageView];
    self.messageView.center = CGPointMake(SCREEN_WIDTH_ZH/2,  SCREEN_HEIGHT_ZH *0.85);
}
// buttonView单例
- (void)buttonActionz:(UIButton *)btn{
    
    ZHPlayViewController *player = [ZHPlayViewController sharaMusicPlayer];
    ZHNavigationBarController *naVC = [[ZHNavigationBarController alloc] initWithRootViewController:player];
    
    [self presentViewController:naVC animated:YES completion:^{
        
    }];
    
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    
#pragma mark - 创建UITableView
    self.listTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
    [self.view addSubview:self.listTableView];
    self.listTableView.dataSource = self;
    self.listTableView.delegate = self;
    self.listTableView.backgroundColor = [UIColor whiteColor];
    self.listTableView.rowHeight = 120;
    // 注册
    [self.listTableView registerClass:[ZTListTableViewCell class] forCellReuseIdentifier:@"ZTListTableViewCellm"];
    
    self.listTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.listTableView reloadData];
    [self getData];
    
    // 返回按钮
    UIButton * backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(20, 0 , 20, 30);
    backButton.backgroundColor = [UIColor clearColor];
    [backButton setImage:[UIImage imageNamed:@"btn_back_n@2x.png"] forState:UIControlStateNormal];
    [backButton setImage:[UIImage imageNamed:@"btn_back_h@2x.png"] forState:UIControlStateHighlighted];
    [backButton addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    //    [_tableHeadView bringSubviewToFront:backButton];
    backButton.tag = 6666;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];

    
    
}
#pragma mark - 返回按钮方法
-(void)backButtonAction:(UIButton *)btn
{
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
}

#pragma mark - 请求数据
-(void)getData
{
    
    
    NSString *urlStr = [NSString stringWithFormat:@"http://mobile.ximalaya.com/m/explore_user_list?category_name=all&condition=%@&device=android&page=1&per_page=20", _titleName];
//    NSLog(@"url -- %@",urlStr);
    [AppTools getDataWithURL:urlStr Block:^(id result) {
        
        //初始化数组
        self.topListArray = [NSMutableArray array];
//         NSLog(@"url -- %@",result);
        
        NSMutableArray *listArray = [result objectForKey:@"list"];

        for (NSMutableDictionary *topDic in listArray) {
            
            ZHListModel *model = [ZHListModel modelWithDictionary:topDic];
//            NSLog(@"url -- %@",model.uid);

            [self.topListArray addObject:model];
//            NSLog(@"url -- %@",self.topListArray);
        }
        
        [_listTableView reloadData];
        
    } ErrorBlock:^(NSError *error) {
        
        NSLog(@"error = %@", error);
    }];
    
}



#pragma mark - tableView协议方法


// row数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.topListArray.count;
}


// 创建cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZTListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZTListTableViewCellm"];
    
    ZHListModel *model = [self.topListArray objectAtIndex:indexPath.row];
    
    NSLog(@"url -- %@",model.uid);
    cell.model = model;

    return cell;
}




// 点击cell触发
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    
    ZHListModel *model = [self.topListArray  objectAtIndex:indexPath.row];
    
    ZHAnchorDetailViewController *listDetailVC = [[ZHAnchorDetailViewController alloc] init];
    // 传值
    listDetailVC.uid = [model.uid integerValue];
    
    
    [(ZHTabBarController *)self.tabBarController isHiddenTabBar:YES withViewController:listDetailVC];
    
    [self.navigationController pushViewController:listDetailVC animated:YES];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
